package com.rbtnet.ssc;

import com.rbtnet.ssc.banker.Banker;
import com.rbtnet.ssc.banker.provider.B365Provider;
import java.util.Map;
import java.util.Scanner;

/**
 * 
 * @author 1136009436@qq.com
 *
 */
public class Client {
	
	private static int jiabei = 5; // 加倍
	private static Double account = 0.0;
	private static final String WIN = "win";
   	private static Long win = 1L;
	
    public static void main(String[] args) {
        Banker banker = new B365Provider();
        RedisService redisService = RedisService.instance();
        redisService.set(WIN, "0");
    	Scanner in = new Scanner(System.in);
    	System.out.println("用户名：");
        String username = in.nextLine();
        System.out.println("密码：");
        String password = in.nextLine();
        System.out.println("下注单位(分-100；角-10):");
        int unit = in.nextInt();
        System.out.println("加倍数：");
        jiabei = in.nextInt();
        banker.login(username, password);
        
        account = banker.balance(); // 初始账号余额
        
        Double initMoney = account;

        while(true){
        	Map<String,String> map = banker.getCountdown();
            String lottyid = map.get("lottyid");
            String adv = banker.getAdv();
            
            Map<String,String> map2 = banker.getFfc();
            String bill = map2.get("bill");
            System.out.println("加倍:"+jiabei);
            
            String data = "[\"37|1|112345|"+jiabei+"|110|5|"+unit+"|,,,,"+bill+"\"]";  //5分
            
            if(map.get("round").equals(map2.get("round")) && "true".equals(map2.get("open"))){
            	if(banker.balance() <= account){
                	win = redisService.decr(WIN);
                	if(win <= 0){
                		redisService.set(WIN, "0");
                		win = 0L;
                		banker.doBet(lottyid, data, adv);
                	}else{
                		System.out.println("获胜次数太多，庄家要坑你了，所以不下单，闪躲剩余次数:"+win);
                	}
                }else{
                	win = redisService.incr(WIN);
                	banker.doBet(lottyid, data, adv);
                }
            }else{
            	if(!map.get("round").equals(map2.get("round"))){
            		try {
                		System.out.println("本期过期下单失败");
    					Thread.sleep(1000*5);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
            	}else{
            		System.out.println("目前胜率太低，休息中。。。");
            	}
            	
            }
            
            account = banker.balance();
            System.out.println("余额:"+account);
            System.out.println("盈利:"+(account-initMoney));
            System.out.println("闪躲剩余次数:"+win+"把");
            System.out.println("---------------------------------------");;
            try {
				Thread.sleep(1000*60);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
    }  

}
